package com.meihua.baoleyi.fragment.sickness;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.meihua.baoleyi.R;
import com.meihua.baoleyi.base.BaseFragment;
import com.meihua.baoleyi.common.APIServer;
import com.meihua.baoleyi.net.NetHelper;

import org.json.JSONObject;

import java.util.TreeMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 治疗方案
 * Created by liming on 2017/10/19.
 */

public class TherapFragment extends BaseFragment implements View.OnClickListener {

    @Bind(R.id.frameLayout)
    FrameLayout mFrameLayout;
    @Bind(R.id.tv_tip)
    TextView tvTip;

    private Context mcontext;
    private static TreeMap<String, String> mTreeMap;

    public static TherapFragment newInstance(TreeMap<String, String> treeMap, int i) {
        TherapFragment fragment = new TherapFragment();
        Bundle mBundle = new Bundle();
        mBundle.putInt("ye_status", i);
        fragment.setArguments(mBundle);
        mTreeMap = treeMap;
        return fragment;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mcontext = activity;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.therap_fragment, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getServerData();
    }

    public void getServerData() {
        TreeMap<String, String> treeMap = new TreeMap<String, String>();
        int ye_status = getArguments().getInt("ye_status");
        treeMap.put("ye_status", ye_status + "");//类型
        treeMap.putAll(mTreeMap);
        NetHelper.post(APIServer.SUM_INFANTS, treeMap, true, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                String sch_name = "";
                try {
                    JSONObject obj = new JSONObject(s);
                    sch_name = obj.getJSONObject("data").get("sch_name").toString();
                    if (obj.optString("code").equals("100")) {
                        //请求成功
                        tvTip.setVisibility(View.GONE);
                        if (sch_name.equals("0")) { //治疗贴
                            TherapCureFragment1 fragment = TherapCureFragment1.newInstance(s);
                            getActivity().getSupportFragmentManager().beginTransaction().add(R.id.frameLayout, fragment, "TherapCureFragment1").commitAllowingStateLoss();
                        } else if (sch_name.equals("1")) { //推拿方案
                            TherapManipulationFragment fragment = TherapManipulationFragment.newInstance(s);
                            getActivity().getSupportFragmentManager().beginTransaction().add(R.id.frameLayout, fragment).commitAllowingStateLoss();
                        }
                    } else {
                        tvTip.setVisibility(View.VISIBLE);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(mcontext,"服务器异常",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
